#!/usr/bin/env python
from accounts.utils.data_analysis import SurveyAnalyzer
import django
import os
import sys
from pathlib import Path

# 获取项目根目录
BASE_DIR = Path(__file__).resolve().parent.parent

# 将项目根目录添加到Python路径
if str(BASE_DIR) not in sys.path:
    sys.path.insert(0, str(BASE_DIR))

# 设置Django环境
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'datacharts.settings')

django.setup()


def main():
    """运行数据分析"""
    analyzer = SurveyAnalyzer()

    # 生成报告
    report = analyzer.generate_report()

    # 打印基本统计信息
    print("\n=== 基本统计信息 ===")
    stats = report['basic_stats']
    print(f"总样本数: {stats['total_count']}")
    print("\n性别分布:")
    for gender in stats['gender_distribution']:
        print(f"{gender['gender']}: {gender['count']}")

    print("\n年龄统计:")
    age_stats = stats['age_stats']
    print(f"平均年龄: {age_stats['avg_age']:.2f}")
    print(f"年龄范围: {age_stats['min_age']} - {age_stats['max_age']}")
    print(f"标准差: {age_stats['std_age']:.2f}")

    # 打印兴趣分析
    print("\n=== 兴趣分析 ===")
    interests = report['interests']
    print("\n运动兴趣平均分:")
    for field, value in interests['sports_avg'].items():
        print(f"{field}: {value:.2f}")

    print("\n艺术兴趣平均分:")
    for field, value in interests['arts_avg'].items():
        print(f"{field}: {value:.2f}")

    # 打印宗教分析
    print("\n=== 宗教信仰分析 ===")
    for field, value in report['religious'].items():
        print(f"{field}: {value:.2f}")

    # 打印个性特征分析
    print("\n=== 个性特征分析 ===")
    for field, value in report['personality'].items():
        print(f"{field}: {value:.2f}")

    # 打印风险行为分析
    print("\n=== 风险行为分析 ===")
    for field, value in report['risk_behaviors'].items():
        print(f"{field}: {value:.2f}")

    # 打印聚类结果
    print("\n=== 聚类分析结果 ===")
    print(report['clusters'])

    print("\n=== 可视化图表 ===")
    print("图表已保存在 static/analysis/ 目录下：")
    print("1. age_distribution.png - 年龄分布图")
    print("2. gender_distribution.png - 性别分布饼图")
    print("3. interests_radar.png - 兴趣雷达图")
    print("4. correlation_heatmap.png - 特征相关性热力图")


if __name__ == '__main__':
    main()
